package com.person.jc.service.base.standard;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class BdStandardGasService extends Service {

	public final static BdStandardGasService me = new BdStandardGasService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select * from bd_standard_gas where if_valid = 1");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("gasName")))){
			sql.append(" and gas_name like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("gasId")))){
			sql.append(" and gas_id = '"+ temp +"'");
		}
		sql.append(" order by gas_name ");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		String sql = "insert into bd_standard_gas (gas_name, measure_scope, remark) values (?, ?, ?)";
		Db.update(sql, map.get("gasName"), map.get("measureScope"), map.get("remark"));
		super.setCRUD("标准气体基础表", "C", "新增标准气体："+map.get("gasName"));
	}
	
	public void update(Map<String,Object> map){
		String sql = " update bd_standard_gas set gas_name = ? , measure_scope = ?, remark = ? where gas_id = ?";
		Db.update(sql, map.get("gasName"), map.get("measureScope"), map.get("remark"), map.get("gasId"));
		super.setCRUD("标准气体基础表", "U", "修改标准气体，编号："+map.get("gasId"));
	}
	
	public void delete(String gasId){
		String sql = " update bd_standard_gas set if_valid = 0 where gas_id = ? ";
		Db.update(sql, gasId);
		super.setCRUD("标准气体基础表", "D", "删除标准气体，编号："+gasId);
	}
	
	public long checkName(String gasName, String gasId){
		String sql = "select count(1) from bd_standard_gas where gas_name = ? and if_valid <> 0 ";
		if(Utils.isNotEmpty(gasId)){
			sql += " and gas_id <> " + gasId;
		}
		return Db.queryLong(sql, gasName);
	}
}
